package ww;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;

public class yinghan extends JFrame {

	private JPanel contentPane;
	private JTextField input;
	private JTextField output;
    private LinkedList<Record>content=new LinkedList<Record>();
	/**
	 * Launch the application. 
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					yinghan frame = new yinghan();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public yinghan() {
		Record r[] = new Record[4];
        r[0] = new Record("hello", "你好");
        r[1] = new Record("world", "世界");
        for (Record i : r)
            content.add(i);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		input = new JTextField();
		input.setBounds(69, 42, 184, 28);
		contentPane.add(input);
		input.setColumns(10);		
		JButton search = new JButton("search");
		search.setBounds(277, 44, 103, 26);
		contentPane.add(search);
		
		JTextArea output = new JTextArea();
		output.setBounds(36, 93, 379, 160);
		contentPane.add(output);
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 String str = input.getText();
			        Iterator<Record> it = content.iterator();
			        Record re;
			        String regex1 = "[A-Za-z]+";
			        String regex2 = "[\u4e00-\u9fa5]+";
			        boolean flag = true;
			        if(str.matches(regex2)||str.matches(regex1)) {
			            if (str.matches(regex2)) {
			                while (it.hasNext()) {
			                    re = it.next();
			                    if (re.Chinese.equals(str)) {
			                        flag = false;
			                        output.setText(re.English);
			                    }
			                    if (flag == true)
			                    	output.setText("no search");
			                }
			            } else if (str.matches(regex1)) {
			                while (it.hasNext()) {
			                    re = it.next();
			                    if (re.English.equals(str)) {
			                        flag = false;
			                        output.setText(re.Chinese);
			                    }
			                    if (flag == true)
			                        output.setText("no search");
			                }
			            }}
			        else {
			        	output.setText("请输入中文或者英文");
			        	 JOptionPane.showMessageDialog(output, "格式不匹配", "消息对话框",
			                     JOptionPane.WARNING_MESSAGE);			      
			        }
			    
			}
		});
		
	}
}
